using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace LDNS.SDK.Commands.Models
{
    /// <summary>
    /// 清空DNS缓存命令类
    /// 作者: huobing
    /// </summary>
    public sealed class ClearCacheCommand : BaseCommand<ClearCacheResult>
    {
        /// <summary>
        /// 命令名称
        /// </summary>
        public const string NAME = "CLEAR_CACHE";
        
        /// <summary>
        /// 获取命令名称
        /// </summary>
        public override string Name => NAME;
        
        /// <summary>
        /// 获取命令格式
        /// </summary>
        public override string Format => "CLEAR_CACHE";
        
        /// <summary>
        /// 获取命令描述
        /// </summary>
        public override string Description => "清空DNS缓存";
        
        /// <summary>
        /// 获取命令权限级别
        /// </summary>
        public override CommandPermission Permission => CommandPermission.Admin;
        
        /// <summary>
        /// 验证命令参数
        /// </summary>
        /// <param name="args">命令参数数组</param>
        /// <param name="error">错误信息输出</param>
        /// <returns>验证是否通过</returns>
        public override bool Validate(string[] args, out string error) 
        {
            error = string.Empty;
            return args.Length == 0 || (error = "此命令不接受参数") == string.Empty;
        }
    }
    
    /// <summary>
    /// 清除缓存命令执行结果
    /// 作者：huobing
    /// </summary>
    public class ClearCacheResult : BaseCommandResult
    {
        /// <summary>
        /// 清除的缓存项数量
        /// </summary>
        public int ClearedCount { get; set; }
        
        /// <summary>
        /// 控制台输出格式化
        /// </summary>
        /// <returns>格式化后的缓存清除结果</returns>
        public override string ToConsoleString()
        {  
            var sb = new System.Text.StringBuilder();
            
            if (!string.IsNullOrEmpty(Message))
            {
                sb.AppendLine(Message);
            }
            else
            {
                sb.AppendLine("缓存清除结果：");
            }
            
            sb.AppendLine($"已清除的缓存项数量: {ClearedCount}");
            
            return sb.ToString().TrimEnd();
        }
    }
}